package cn.brian.modular.sys.consumer;


import cn.brian.modular.sys.entity.SysUser;
import cn.brian.modular.sys.service.SysUserService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

/**
 * @author Administrator
 * @version 1.0
 **/
@Component
@RocketMQMessageListener(consumerGroup = "consumer_group_txmsg_bank2",topic = "topic_txmsg")
public class TxmsgConsumer implements RocketMQListener<MessageExt> {

    @Autowired
    SysUserService sysUserService;
    @Autowired
    private ServiceImpl serviceImpl;
    //接收消息
    @Override
    public void onMessage(MessageExt message) {
        System.out.printf("开始消费消息:{%s}",message);
        //解析消息
        String messageString = new String((byte[]) message.getBody());
        JSONObject jsonObject = JSONObject.parseObject(messageString);
        String sysUserString = jsonObject.getString("sysUser");
        String classString=jsonObject.getString("dataobject");
        System.out.printf(classString);

        Object c= null;
        try {
            c = Class.forName(classString).newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        //sysUserStr（json）SysUser
        Object sysUser = JSONObject.parseObject(sysUserString,c.getClass());

        //转成AccountChangeEvent
//        SysUser sysUser = JSONObject.parseObject(sysUserString, SysUser.class);

        serviceImpl.save(sysUser);


        //更新时要判断最后的时间戳是否大于当前数据库的数据的时间
    }
}
